package bar.dark.lee.common;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Pattern;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/**
 * EditText 필터링
 * @author hyojun
 */
public class EditTextInputFilter {
	
	public enum TYPE { KOREAN, ENGLISH, ENGLISH_NUMBER, ENGLISH_NUMBER_ETC, NOT_FIRST_ZERO, };
	private static String pattern;
	
	public EditTextInputFilter() {
	}
	
	/**
	 * @param et EditText
	 * @param type 필터링타입(EditTextInputFilter.TYPE.타입)
	 */
	public static void filtering(final EditText et, final TYPE type)
	{
		switch (type) {
		case KOREAN:
			pattern = "^[ㄱ-ㅣ가-힣ᆞᆢ]*$";
//			pattern = "^[^a-zA-Z0-9\\W]*$";
			break;
		case ENGLISH:
			pattern = "^[a-zA-Z]*$";
			break;
		case ENGLISH_NUMBER:
			pattern = "^[a-zA-Z0-9]*$";
			break;
		case ENGLISH_NUMBER_ETC:
//			pattern = "^[a-zA-Z0-9_]*$";
			pattern = "^[^\\sㄱ-ㅣ가-힣]*$";
			break;
		default:
			break;
		}
		et.addTextChangedListener(new TextWatcher() 
		{
			String pattern = EditTextInputFilter.pattern;
			String beforeStr;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
				if(count == 0)
					return;
				
				boolean result = Pattern.matches(pattern, s);
				if(s.length() > 0 && result)
				{
					return;
				}
				else
				{
					et.setText(beforeStr);
					try {
						et.setSelection(et.getText().toString().length());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after)
			{
				beforeStr = s.toString();
			}
			@Override
			public void afterTextChanged(Editable s)
			{
			}
		});
	}
	
	/**
	 * @param et EditText
	 */
	public static void filteringMoneyEditText(final EditText et) {
		
		et.addTextChangedListener(new TextWatcher() 
		{
			String beforeStr = "";
			
			String temp = "";
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(count == 0)
				{
					return;
				}
				
//				if(beforeStr.length() < 1 && s.toString().equals("0")) {
//					et.setText(beforeStr);
//					try {
//						et.setSelection(et.getText().toString().length());
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//					return;
//				}
//				else {
//					return;
//				}
				
				if ( et.getText().toString().length() > 1) 
				{
//					if (beforeStr.equals("0")) 
//					{
//						if (!et.getText().toString().equals("0."))
//						{
//							et.setText("0");
//							try {
//								et.setSelection(et.getText().toString().length());
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//						}
//						
//					}
//					else if (beforeStr.equals("-0"))
//					{
//						if (!et.getText().toString().equals("-0."))
//						{
//							et.setText("0");
//							
//							try {
//								et.setSelection(et.getText().toString().length());
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//						}
//						
//					}
					
					int flag = et.getText().toString().indexOf(".");
					
					if (flag > -1)
					{
						String [] tempStr = et.getText().toString().split("\\.");
						
						if (tempStr.length > 1)
						{
							if (tempStr[1].length() > 3)
							{
								et.setText(beforeStr);
							}
							
							if (tempStr[0].startsWith("0") && tempStr[0].length() > 1)
							{
								et.setText(beforeStr);
							}
							
							try {
								et.setSelection(et.getText().toString().length());
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					
					beforeStr = et.getText().toString();
				}
				else
				{
					if (s.toString().equals("."))
					{
						et.setText("");
					}
					else
					{
						beforeStr = s.toString();
					}
				}
				
				if (!s.toString().equals(temp))
				{
					if (s.toString().indexOf(".") > -1)
					{
						String [] str = s.toString().split("\\.");
						
						if (str.length > 1)
						{
							if (str[1].length() > 0)
							{
								return;
							}
						}
					}
					
					if (s.toString().equals("-"))
					{
						return;
					}
					
					temp = makeStringWithComma(s.toString(), false);
					
					et.setText(temp);
					
					try {
						et.setSelection(et.getText().toString().length());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) 
			{
			}
			@Override
			public void afterTextChanged(Editable s) 
			{
			}
		});
		
		et.setOnKeyListener(new OnKeyListener()
		{
			String temp = "";
			
			int count = 0;
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event)
			{
				if (keyCode == KeyEvent.KEYCODE_DEL)
				{
					EditText et = (EditText) v;
					
					if (!et.getText().toString().equals(temp))
					{
						if (et.getText().toString().indexOf(".") > -1)
						{
							String [] str = et.getText().toString().split("\\.");
							
							if (str.length > 1)
							{
								if (str[1].length() > 0)
								{
									return false;
								}
							}
						}
						
						if (et.getText().toString().equals("-"))
						{
							return false;
						}
						
						String str = et.getText().toString();
						
//						str = str.substring(0, str.length()-1);
						
						temp = makeStringWithComma(str, false);
						
						count = et.getSelectionEnd();
						
						et.setText(temp);
					}
					
					if (et.getText().toString().length() >= count)
					{
						try {
							et.setSelection(count);
						} catch (Exception e) {
							e.printStackTrace();
							return false;
						}
					}
				}
				return false;
			}
		});
	}
	
	public static String makeStringWithComma(String name, boolean ignoreZero)
	{
		String string = name;
		
		if (string.length() == 0)
		{
			return "";
		}
		
		try
		{
			if (string.indexOf(".") > -1)
			{
//				BigDecimal value = new BigDecimal(string.replace(",", ""));
//					
//				if (ignoreZero && value.compareTo(BigDecimal.ZERO) == 0)
//				{
//					return "";
//				}
//				
//				DecimalFormat format = new DecimalFormat("###,###.000");
//				
//				return format.format(string);
				
				return string;
			}
			else
			{
				BigDecimal value = new BigDecimal(string.replace(",", ""));
				
				if (ignoreZero && value.compareTo(BigDecimal.ZERO) == 0)
				{
					return "";
				}
				
				DecimalFormat format = new DecimalFormat("###,###");
				return format.format(value);    
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return string;
	}



	
	/**
	 * @param et EditText
	 */
	public static void filteringZero(final EditText et) {
		
		et.addTextChangedListener(new TextWatcher() 
		{
			String beforeStr="";
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(count == 0)
					return;
				
				if(beforeStr.length() < 1 && s.toString().equals("0")) {
					et.setText(beforeStr);
					try {
						et.setSelection(et.getText().toString().length());
					} catch (Exception e) {
						e.printStackTrace();
					}
					return;
				}
				else {
					return;
				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) 
			{
				
			}
			@Override
			public void afterTextChanged(Editable s) 
			{
				
			}
		});
	}
}
